package com.yzl.meeting.business.service.Impl;

import com.yzl.meeting.business.dao.RolePermissionDao;
import com.yzl.meeting.business.dao.impl.RolePermissionDaoImpl;
import com.yzl.meeting.business.entity.Permission;
import com.yzl.meeting.business.entity.Role;
import com.yzl.meeting.business.service.RolePermissionService;
import com.yzl.meeting.business.utils.MyDbUtils;
import com.yzl.meeting.business.vo.RolePerVo;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Title: RolePermissionServiceImpl
 * @Author: 姚志立
 * @Description:
 * @Date: 2020/10/12 16:43
 * @Version: 1.0
 */
public class RolePermissionServiceImpl implements RolePermissionService {
    RolePermissionDao rolePermissionDao = new RolePermissionDaoImpl();

    @Override
    public int addPer(Permission permission) throws Exception {
        return rolePermissionDao.addPer(permission);
    }

    @Override
    public void addRole(RolePerVo rolePerVo) throws Exception {
        try {
            MyDbUtils.beginTransaction();
            Integer[] perIds = rolePerVo.getPermIds();
            int roleId = rolePermissionDao.addRole(rolePerVo);


            if (perIds != null) {
                for (int i = 0; i < perIds.length; i++) {
                    rolePermissionDao.addRolePer(roleId, perIds[i]);
                }
            }
            MyDbUtils.commitTransaction();
        }catch (Exception e){
            MyDbUtils.rollbackTransaction();
            e.printStackTrace();
            throw new Exception(e);
        }

    }

    @Override
    public List<Role> listRoles() throws Exception {
        return rolePermissionDao.selectAllRole();
    }

    @Override
    public List<Permission> listPer() throws Exception {
        return rolePermissionDao.selectAllPer();
    }

    @Override
    public void deletePer(Integer perId) throws SQLException {
        rolePermissionDao.deletePer(perId);
    }

    @Override
    public void deleteRole(Integer roleId) throws SQLException {
        rolePermissionDao.deleteRolePer(roleId);
        rolePermissionDao.deleteRole(roleId);
    }
}
